package cn.dark.auth.login;

import cn.dark.auth.core.LoginCore;
import cn.dark.auth.core.exception.AuthBaseException;
import cn.dark.auth.util.SpringMvcUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author dark
 * @date 2023-10-28
 */
@RestController
public class SimpleLoginController {

    @PostMapping("/login")
    public String login(@RequestBody LoginDTO loginDTO) {
        if (Objects.isNull(loginDTO.getUserId()) || StringUtils.isBlank(loginDTO.getPassword())) {
            throw new AuthBaseException("登录失败，未传入userId和密码");
        }

        // 忽略账号的校验，密码写死
        if (!loginDTO.getPassword().equals("1212")) {
            throw new AuthBaseException("密码错误！");
        }
        return LoginCore.login(loginDTO.getUserId(), loginDTO.getDevice());
    }

    @GetMapping("/isLogin")
    public boolean isLogin(HttpServletRequest request) {
        return LoginCore.isLogin(SpringMvcUtils.getToken());
    }

}
